package org.third.spring.boot.hello.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartRequest;
import org.third.spring.boot.hello.domain.HelloFeature;
import org.third.spring.boot.hello.domain.HelloUser;
import org.third.spring.boot.hello.repository.HelloFeatureRepository;
import org.third.spring.boot.hello.repository.HelloUserRepository;
import org.third.spring.boot.hello.service.HelloUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Controller
@RequestMapping(value = "/api/v1/users")
public class HelloUserController {
	@Autowired
	HelloUserService helloUserService;
	@Autowired
	HelloUserRepository userRepository;
	@Autowired
	HelloFeatureRepository featureRepository;
	static Map<String, HelloUser> USERS = new ConcurrentHashMap<>();
	static {
		USERS.put("test1", new HelloUser("test1"));
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	List<HelloUser> getUsers() {
		List<HelloUser> users = helloUserService.listUsers();
		System.out.println(helloUserService);
		return users;
	}

	@RequestMapping(value = "{userId}", method = RequestMethod.GET)
	@ResponseBody
	HelloUser getUser(String userId) {
		List<HelloUser> users = helloUserService.listUsers();
		System.out.println(helloUserService);
		return USERS.get(userId);
	}

	@RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	@ResponseBody
	HelloUser postUser(@RequestBody HelloUser user) {
		String userId = user.getId() == null ? UUID.randomUUID().toString() : user.getUuid();
		System.out.println("Creating: " + user);
		user.setUuid(userId);
		USERS.put(userId, user);
		return user;
	}

	@RequestMapping(value = "{userId}/features", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	@ResponseBody
	HelloFeature postUserFeature(@RequestBody HelloFeature feature) {
		HelloFeature user = null;
		return user;
	}

	@RequestMapping(value = "{userId}/features", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
	@ResponseBody
	List<HelloFeature> getUserFeatures(@RequestBody String userId) {
		Iterable<HelloFeature> featureIterator = featureRepository.findAll();
		List<HelloFeature> features = new ArrayList<HelloFeature>();
		featureIterator.forEach(c -> {
			features.add(c);
		});
		return features;
	}

	@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	HelloUser deleteUser(String userId) { 
		System.out.println("Deleting: " + userId);
		return USERS.get(userId);
	}

	@RequestMapping(value = "/{id}/photo", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public HelloUser uploadMultiFiles(
			// @RequestParam("file") MultipartFile[] files,
			MultipartRequest request, @RequestHeader(value = "x-auth-token", required = false) String idmToken,
			@RequestParam("key") String key, @RequestHeader(value = "csrf_token") String csrfToken) {

		return new HelloUser();

	}
}
